package com.zhuiyun.project.api.courseSubscribe.service.impl;

import com.zhuiyun.project.api.courseSubscribe.entity.CourseSubscribe;
import com.zhuiyun.project.api.courseSubscribe.mapper.CourseSubscribeMapper;
import com.zhuiyun.project.api.courseSubscribe.model.CourseSubscribeModel;
import com.zhuiyun.project.api.courseSubscribe.model.CourseUpdateStatusModel;
import com.zhuiyun.project.api.courseSubscribe.service.CourseSubscribeService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.BeanUtils;
import com.zhuiyun.project.utils.ObjectUtils;
import com.zhuiyun.project.utils.StringUtils;
import com.zhuiyun.project.utils.UploadFileUtils;
import com.zhuiyun.project.utils.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import com.zhuiyun.project.utils.pageUtil.PageResult;
import com.zhuiyun.project.utils.validator.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.swing.*;
import java.util.Date;
import java.util.List;

/**
 * @ClassName CourseSubscribeServiceImpl
 * @Description TODO
 * @Author 胡家浩
 * @Date 2024/5/31 9:55
 **/
@Service
public class CourseSubscribeServiceImpl implements CourseSubscribeService {


    @Autowired
    private CourseSubscribeMapper courseSubscribeMapper;

    /**
     * @param pageRequest
     * @param venueUniqueIdentifier
     * @param courseName
     * @Author 胡家浩
     * @Description 查询约课信息
     * @Date 2024/5/31 10:37
     * @Return com.zhuiyun.project.common.response.CommonResult
     */
    @Override
    public CommonResult getCourseList(PageRequest pageRequest, String venueUniqueIdentifier, String courseName) {
        if (ObjectUtils.isNotEmpty(pageRequest)) {
            MybatisPageHelper.startPage(pageRequest);
            List<CourseSubscribe> courseList = courseSubscribeMapper.getCourseList(venueUniqueIdentifier, courseName);
            PageResult pageResult = MybatisPageHelper.getPageResult(courseList);
            return CommonResult.ok(pageResult);
        } else {
            return CommonResult.error("分页参数不能为空");
        }
    }

    /**
     * @param id
     * @param venueUniqueIdentifier
     * @Author hjh
     * @Description 根据id获取课程信息
     * @Date 2024/5/31 15:35
     * @Return com.zhuiyun.project.common.response.CommonResult
     */
    @Override
    public CommonResult getCourseInfoById(String id, String venueUniqueIdentifier) {
        if (StringUtils.isNotEmpty(id) && StringUtils.isNotEmpty(venueUniqueIdentifier)) {
            CourseSubscribe courseSubscribeInfo = courseSubscribeMapper.getCourseInfoById(id, venueUniqueIdentifier);
            return CommonResult.ok(courseSubscribeInfo);
        } else {
            return CommonResult.error("参数不能为空");
        }
    }

    /**
     * @param courseSubscribeModel
     * @Author hjh
     * @Description 新增约课信息
     * @Date 2024/5/31 16:41
     * @Return com.zhuiyun.project.common.response.CommonResult
     */
    @Override
    public CommonResult addCourse(MultipartFile[] multipartFiles, CourseSubscribeModel courseSubscribeModel) throws CommonException {
        if (ValidatorUtil.ValidationObject(courseSubscribeModel)) {
            CourseSubscribe courseSubscribe = new CourseSubscribe();
            BeanUtils.copy(courseSubscribeModel, courseSubscribe);
            StringBuilder stringBuilder = new StringBuilder();
            if (ObjectUtils.isNotEmpty(multipartFiles)) {
                for (MultipartFile multipartFile : multipartFiles) {
                    String i = UploadFileUtils.uploadFile(multipartFile);
                    if (stringBuilder.length() > 0) {
                        stringBuilder.append("," + i);
                    } else {
                        stringBuilder.append(i);
                    }
                }
            }
            //添加图片数组
            if (stringBuilder.length() > 0) {
                courseSubscribe.setCoverPic(stringBuilder.toString());
            } else {
                courseSubscribe.setCoverPic("");
            }
            courseSubscribe.setCourseCreateTime(new Date());
            courseSubscribe.setCourseStatus(0);
            int insert = courseSubscribeMapper.insert(courseSubscribe);
            if (insert > 0) {
                return CommonResult.ok(insert);
            } else {
                return CommonResult.ok(EmErrorCode.SAVE_ERROR);
            }
        } else {
            return CommonResult.ok(EmErrorCode.SAVE_ERROR);
        }
    }

    /**
    * @Author hjh
    * @Description 更新课程状态
    * @Date 2024/6/3 9:34
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param courseSubscribeModel
    */
    @Override
    public CommonResult updateCourseStatus(CourseUpdateStatusModel courseSubscribeModel) throws CommonException {
        if (ValidatorUtil.ValidationObject(courseSubscribeModel)){
            Integer courseStatus = courseSubscribeModel.getCourseStatus();
            Integer id = courseSubscribeModel.getId();
            String venueUniqueIdentifier = courseSubscribeModel.getVenueUniqueIdentifier();
            int i = courseSubscribeMapper.updateCourseStatus(id,courseStatus,venueUniqueIdentifier);
            return CommonResult.ok(i);
        }else {
            return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
        }
    }

    /**
    * @Author hjh
    * @Description 修改课程信息
    * @Date 2024/6/3 11:26
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param courseSubscribeModel
    */
    @Override
    public CommonResult updateCourseById(MultipartFile [] files,CourseSubscribeModel courseSubscribeModel) throws CommonException{
        if (ValidatorUtil.ValidationObject(courseSubscribeModel)){
            StringBuilder stringBuilder = new StringBuilder();
            if (StringUtils.isNotEmpty(courseSubscribeModel.getCoverPic())){
                stringBuilder.append(courseSubscribeModel.getCoverPic());
            }
            if (ObjectUtils.isNotEmpty(files)) {
                for (MultipartFile multipartFile : files) {
                    String i = UploadFileUtils.uploadFile(multipartFile);
                    if (stringBuilder.length() > 0) {
                        stringBuilder.append("," + i);
                    } else {
                        stringBuilder.append(i);
                    }
                }
            }
            CourseSubscribe courseSubscribe = new CourseSubscribe();
            BeanUtils.copy(courseSubscribeModel,courseSubscribe);
            //添加图片数组
            if (stringBuilder.length() > 0) {
                courseSubscribe.setCoverPic(stringBuilder.toString());
            } else {
                courseSubscribe.setCoverPic("");
            }
            int i = courseSubscribeMapper.updateCourseById(courseSubscribe);
            return CommonResult.ok(i);
        }else {
            return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
        }
    }

    /**
    * @Author hjh
    * @Description 删除课程信息
    * @Date 2024/6/3 15:01
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param id
* @param venueUniqueIdentifier
    */
    @Override
    public CommonResult delCourse(Integer id, String venueUniqueIdentifier) {
        if (ObjectUtils.isNotEmpty(id) && StringUtils.isNotEmpty(venueUniqueIdentifier)){
            int i = courseSubscribeMapper.delCourse(id, venueUniqueIdentifier);
            return CommonResult.ok(i);
        }else {
            return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
        }
    }
}
